<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="where" label-width="77px" class="ele-form-search" @keyup.enter.native="reload" @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="产品分类:">
              <el-input clearable v-model="where.name" placeholder="请输入分类名称" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button type="primary" icon="el-icon-search" class="ele-btn-icon" @click="reload">查询</el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :where="where" row-key="id" :datasource="url" :columns="columns" default-expand-all :need-page="false" :parse-data="parseData" height="calc(100vh - 270px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button size="small" type="primary" icon="el-icon-plus" class="ele-btn-icon" @click="openEdit(null)" v-if="permission.includes('sys:dept:add')">
            添加
          </el-button>
          <el-button @click="expandAll" class="ele-btn-icon" size="small" v-if="permission.includes('sys:dept:expand')">
            展开全部
          </el-button>
          <el-button @click="foldAll" class="ele-btn-icon" size="small" v-if="permission.includes('sys:dept:collapse')">
            折叠全部
          </el-button>
        </template>
        <!-- 标题列 -->
        <template slot="name" slot-scope="{row}">
          <i :class="row.icon"></i> {{ row.name }}
        </template>
        <!-- 状态 -->
        <template slot="status" slot-scope="{row}">
          {{ row.status === 1 ? "启用" : "禁用" }}
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link type="primary" :underline="false" icon="el-icon-edit" @click="openEdit(row)" v-if="permission.includes('sys:dept:edit')">
            修改
          </el-link>
          <el-popconfirm class="ele-action" title="确定要删除吗？" @confirm="remove(row)">
            <el-link type="danger" slot="reference" :underline="false" icon="el-icon-delete" v-if="permission.includes('sys:dept:delete')">
              删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>

    <EditCategory ref="CategoryEdit" :data="current" :visible.sync="showEdit" @addCategories="addCategories" @editCategories="editCategories" />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import EditCategory from "./components/category-edit.vue"

export default {
  name: 'SystemDept',
  components: { EditCategory },
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/categories/tree',
      // 表格列配置
      columns: [
        {
          columnKey: 'index',
          type: 'index',
          width: 45,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'name',
          label: '产品分类',
          showOverflowTooltip: true,
          minWidth: 200,
          slot: 'name',
          fixed: "left"
        },
        {
          prop: 'sort_order',
          label: '排序',
          showOverflowTooltip: true,
          align: 'center',
          minWidth: 110
        },
        {
          prop: 'status',
          label: '状态',
          showOverflowTooltip: true,
          align: 'center',
          minWidth: 200,
          slot: 'status'
        },
        {
          prop: 'create_time_format',
          label: '创建时间',
          showOverflowTooltip: true,
          minWidth: 120
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 190,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 全部部门数据
      deptList: []
    };
  },
  methods: {
    // 新增
    addCategories(value) {
      this.$http.post('/categories', value).then(res => {
        this.$refs.CategoryEdit.loading = false;
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.$refs.CategoryEdit.updateVisible(false);
          this.reload();
        }
      }).catch(e => {
        this.$refs.CategoryEdit.loading = false;
        this.$message.error(e.message);
      });
    },
    // 编辑
    editCategories(value, id) {
      this.$http.put('/categories/' + id, value).then(res => {
        this.$refs.CategoryEdit.loading = false;
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.$refs.CategoryEdit.updateVisible(false);
          this.reload();
        }
      }).catch(e => {
        this.$refs.CategoryEdit.loading = false;
        this.$message.error(e.message);
      });
    },
    /* 解析接口返回数据 */
    parseData(res) {
      res.data = this.$util.toTreeData(res.data, 'id', 'pid');
      this.deptList = res.data;
      return res;
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row, parentId) {
      row === null ? this.current = row : this.current = Object.assign({ level: 1, pid: parentId }, row);
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      if (row.children && row.children.length > 0) {
        this.$message.error('请先删除子节点');
        return;
      }
      const loading = this.$loading({ lock: true });
      this.$http.delete('/categories/' + row.id).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 展开全部 */
    expandAll() {
      this.$refs.table.data.forEach(d => {
        this.$refs.table.toggleRowExpansion(d, true);
      });
    },
    /* 折叠全部 */
    foldAll() {
      this.$refs.table.data.forEach(d => {
        this.$refs.table.toggleRowExpansion(d, false);
      });
    },
  }
}
</script>

<style scoped>
</style>
